/*
 * @Author: szx
 * @Date: 2021-10-25 11:51:37
 * @LastEditTime: 2021-10-25 12:27:26
 * @Description:
 * @FilePath: \leetcode\121\121.go
 */

package main

import (
	"strconv"
	"strings"
)

func maxProfit(prices []int) int {
	res := 0
	low := prices[0]
	for _, p := range prices {
		if p-low > res {
			res = p - low
		} else if p-low < 0 {
			low = p
		}
	}
	return res
}

func generateArr(str string) []int {
	s := strings.TrimLeft(str, "[")
	s = strings.TrimRight(s, "]")
	strArr := strings.Split(s, ",")
	res := make([]int, len(strArr))
	for index, val := range strArr {
		res[index], _ = strconv.Atoi(val)
	}
	return res
}

func main() {
	println(maxProfit(generateArr("[1,0,2]")))
}
